package com.cz.modules.rll.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cz.common.utils.HCKNetSDKUtil;
import com.cz.modules.rll.entity.RllHourCountEntity;
import com.cz.modules.rll.entity.RllPlaceEntity;
import com.cz.modules.rll.service.RllPlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cz.common.utils.PageUtils;
import com.cz.common.utils.Query;

import com.cz.modules.rll.dao.RllDayInfoDao;
import com.cz.modules.rll.entity.RllDayInfoEntity;
import com.cz.modules.rll.service.RllDayInfoService;


@Service("rllDayInfoService")
public class RllDayInfoServiceImpl extends ServiceImpl<RllDayInfoDao, RllDayInfoEntity> implements RllDayInfoService {
    @Autowired
    private RllPlaceService rllPlaceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<RllDayInfoEntity> page = this.page(
                new Query<RllDayInfoEntity>().getPage(params),
                new QueryWrapper<RllDayInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void getTodayKLBySDK() {
        //客流卡口列表
        List<RllPlaceEntity> placeList=rllPlaceService.list();
        for (RllPlaceEntity place:placeList) {
            Integer iUserId=-1;
            if (HCKNetSDKUtil.lUserIDMap.containsKey(place.getCameraIp())){
                iUserId=HCKNetSDKUtil.lUserIDMap.get(place.getCameraIp());
            }
            if(iUserId==-1){
                iUserId=HCKNetSDKUtil.login(place.getCameraIp());
                HCKNetSDKUtil.lUserIDMap.put(place.getCameraIp(),iUserId);
            }
            //获取当前日期
            String date= DateUtil.format(new Date(),"yyyy-MM-dd");
            List<RllHourCountEntity> hourCountList=new ArrayList<>();
            try {
                hourCountList= HCKNetSDKUtil.getPDCResult(place.getCameraIp(),iUserId,date+" 0:0:0",date+ " 23:59:59");

            }catch (Exception e){
                e.printStackTrace();
            }
            if (hourCountList.size()>0){
                int enterNum=0;
                int leaveNum=0;
                for (RllHourCountEntity hourCount: hourCountList) {
                    enterNum+=hourCount.getEnterNum();
                    leaveNum+=hourCount.getLeaveNum();
                }
                this.remove(new QueryWrapper<RllDayInfoEntity>().eq("place_id",place.getId()));
                RllDayInfoEntity dayInfo=new RllDayInfoEntity();
                dayInfo.setPlaceId(place.getId());
                dayInfo.setArea(place.getArea());
                dayInfo.setDataTime(new Date());
                dayInfo.setEnterNum(enterNum);
                dayInfo.setLeaveNum(leaveNum);
                this.save(dayInfo);
            }
        }
    }

    @Override
    public Integer kllTodayCount(List<RllPlaceEntity> list) {
        List<String> palceIdList=new ArrayList<>();
        for (RllPlaceEntity place:list) {
            palceIdList.add(place.getId());
        }
        String startTime=DateUtil.format(new Date(),"yyyy-MM-dd");
        List<RllDayInfoEntity> dayInfoList=this.baseMapper.kllTodayCount(startTime+ " 00:00:00",palceIdList);
        Integer kllTodayCount=0;
        for (RllDayInfoEntity dayInfo:dayInfoList) {
            kllTodayCount+=dayInfo.getEnterNum();
        }
        return kllTodayCount;
    }

}
